package cn.huijielong.common.utils;

import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Base64;
import java.util.Calendar;
import java.util.regex.Pattern;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 加密/解密工具
 */
public class EncryptUtil {
	
	
	private final byte[] DESIV = new byte[] { 0x12, 0x34, 0x56, 120, (byte) 0x90, (byte) 0xab, (byte) 0xcd,
			(byte) 0xef };// 向量

	private AlgorithmParameterSpec iv = null;// 加密算法的参数接口
	private Key key = null;

	private String charset = "utf-8";

	/**
	 * 初始化
	 * 
	 * @param deSkey
	 *            密钥
	 * @throws Exception
	 */
	public EncryptUtil(String deSkey) throws Exception {
		DESKeySpec keySpec = new DESKeySpec(deSkey.getBytes(this.charset));// 设置密钥参数
		iv = new IvParameterSpec(DESIV);// 设置向量
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
		key = keyFactory.generateSecret(keySpec);// 得到密钥对象
	}

	/**
	 * 加密
	 * 
	 * @date 2017年4月19日 上午9:40:53
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public String encode(String data) throws Exception {
		Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");// 得到加密对象Cipher
		enCipher.init(Cipher.ENCRYPT_MODE, key, iv);// 设置工作模式为加密模式，给出密钥和向量
		byte[] pasByte = enCipher.doFinal(data.getBytes("utf-8"));
		BASE64Encoder base64Encoder = new BASE64Encoder();
		return base64Encoder.encode(pasByte);
	}

	/**
	 * 解密
	 * 
	 * @date 2017年4月19日 上午9:41:01
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public String decode(String data) throws Exception {
		boolean isBase64=isBase64(data);
		if(isBase64) {
			Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
			deCipher.init(Cipher.DECRYPT_MODE, key, iv);
			BASE64Decoder base64Decoder = new BASE64Decoder();
			byte[] pasByte = deCipher.doFinal(base64Decoder.decodeBuffer(data));
			return new String(pasByte, "UTF-8");
		}else {
			return data;
		}
	}
	/**
	 * 判断是否经过base64编码
	 * @param str
	 * @return
	 */
	public  boolean isBase64(String str) {
	    String base64Pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
	    return Pattern.matches(base64Pattern, str);
	}
	
	public static String getSecurityKey() {
		Calendar c = Calendar.getInstance();
		StringBuilder src = new StringBuilder();
		int month = c.get(Calendar.MONTH);
		src.append(c.get(Calendar.YEAR));
		if (month < 10)
			src.append(0).append(month);
		else
			src.append(month);
		src.append("www.xietongnet.com");
		String skey = Base64.getEncoder().encodeToString(src.toString().getBytes());
		if (skey.length() > 32)
			return skey.substring(0, 32);
		return skey;
	}

	public static void main(String[] args) {
		try {
			String test = "395,395,4,368,null,4,A2yrbMvj96YdFV3GY4aUl84HQILTKU";
			String key = "9ba45bfd500642328ec03ad8ef1b6e75";// 自定义密钥

			EncryptUtil des = new EncryptUtil(key);
			String skey = des.getSecurityKey();
			System.out.println(skey);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
